<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="65px">
      <el-form-item label="监理机构" prop="jljg">
        <el-input v-model="queryParams.jljg" placeholder="请输入监理机构" clearable @keyup.enter.native="handleQuery"
          style="width: 205px" />
      </el-form-item>
      <el-form-item label="编号" prop="bh">
        <el-input v-model="queryParams.bh" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery"
          style="width: 205px" />
      </el-form-item>
      <el-form-item label="记录人" prop="llr">
        <el-input v-model="queryParams.llr" placeholder="请输入记录人" clearable @keyup.enter.native="handleQuery"
          style="width: 205px" />
      </el-form-item>
      <el-form-item label="审核人" prop="shry">
        <el-input v-model="queryParams.shry" placeholder="请输入审核人" clearable @keyup.enter.native="handleQuery"
          style="width: 205px" />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker v-model="dateRange" style="width: 205px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="">
        <el-checkbox v-model="queryParams.allProject" v-hasPermi="['supervision:log:allProject']">全部项目</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['supervision:log:add']">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['supervision:log:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['supervision:log:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['supervision:log:export']">导出</el-button>
        </el-col> -->
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>
    </el-form>
    <el-table border stripe v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监理机构" align="center" prop="jljg" min-width="160" />
      <el-table-column label="编号" align="center" prop="bh" min-width="120" />
      <el-table-column label="记录人" align="center" prop="llr" min-width="120" />
      <el-table-column label="审核人" align="center" prop="shry" min-width="120" />
      <el-table-column label="日期" align="center" prop="rq" width="120">
        <template slot-scope="scope">
          {{ parseTime(scope.row.rq, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="天气情况" align="center" prop="tqqk" /> -->
      <!-- <el-table-column label="主要施工情况" align="center" prop="zysgqk" /> -->
      <!-- <el-table-column label="监理主要工作" align="center" prop="jlzygz" /> -->
      <!-- <el-table-column label="问题及处理情况" align="center" prop="wtjclqk" /> -->
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['supervision:log:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['supervision:log:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="onPrint(scope.row)"
            v-hasPermi="['supervision:log:export']">打印</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改监理日志对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="0px">
        <el-descriptions border title="" :column="2" size="medium" labelClassName="descriptions-label"
          contentClassName="descriptions-content">
          <el-descriptions-item label="所属项目：" span="2">
            <el-form-item label="" prop="">
              {{ form.projectName }}
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="监理机构：">
            <el-form-item label="" prop="jljg">
              <el-input v-model="form.jljg" placeholder="请输入监理机构" />
              <!-- <el-select v-model="form.jljg" placeholder="请选择单位" style="width: 100%;" filterable
                @change="setDeptName()">
                <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
              </el-select> -->
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="编号：">
            <el-form-item label="" prop="bh">
              <el-input v-model="form.bh" placeholder="请输入编号" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="记录人：">
            <el-form-item label="" prop="llr">
              <el-input v-model="form.llr" placeholder="请输入记录人" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="审核人：">
            <el-form-item label="" prop="shry">
              <el-input v-model="form.shry" placeholder="请输入审核人" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="日期：">
            <el-form-item label="" prop="rq">
              <el-date-picker clearable v-model="form.rq" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期"
                style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="天气情况：">
            <el-form-item label="" prop="tqqk">
              <el-input v-model="form.tqqk" placeholder="请输入天气情况" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="主要施工情况：" span="2">
            <el-form-item label="" prop="zysgqk">
              <el-input type="textarea" rows="3" v-model="form.zysgqk" placeholder="请输入主要施工情况" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="监理主要工作：" span="2">
            <el-form-item label="" prop="jlzygz">
              <el-input type="textarea" rows="3" v-model="form.jlzygz" placeholder="请输入监理主要工作" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="问题及处理情况：" span="2">
            <el-form-item label="" prop="wtjclqk">
              <el-input type="textarea" rows="3" v-model="form.wtjclqk" placeholder="请输入问题及处理情况" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="附件：" span="2">
            <el-form-item label="" prop="fileId">
              <fileUpload v-model="form.fileId" :oldList="form.fileIdOld" :fileType="['doc', 'docx', 'xlsx', 'xls'
                , 'pdf', 'png', 'jpg', 'jpeg']" />
            </el-form-item>
          </el-descriptions-item>
        </el-descriptions>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
    <print-dialog ref="printDialogRef" />
  </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog } from "@/api/supervision/log";
import { listDeptByType } from "@/api/system/dept";
import { mapGetters } from "vuex";
import PrintDialog from './PrintDialog.vue';

export default {
  components: { PrintDialog },
  name: "SupervisionLog",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监理日志表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: undefined,
        projectName: undefined,
        jljg: undefined,
        bh: undefined,
        llr: undefined,
        shry: undefined,
        rq: undefined,
        tqqk: undefined,
        zysgqk: undefined,
        jlzygz: undefined,
        wtjclqk: undefined,
        fileId: undefined,
        allProject: undefined
      },
      deptOptions: [],
      // 附件id时间范围
      dateRange: [],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jljg: [
          { required: true, message: "监理机构不能为空", trigger: "blur" }
        ],
        bh: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        // llr: [
        //   { required: true, message: "记录人不能为空", trigger: "blur" }
        // ],
        // shry: [
        //   { required: true, message: "审核人不能为空", trigger: "blur" }
        // ],
        rq: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        // tqqk: [
        //   { required: true, message: "天气情况不能为空", trigger: "blur" }
        // ],
        // zysgqk: [
        //   { required: true, message: "主要施工情况不能为空", trigger: "blur" }
        // ],
        // jlzygz: [
        //   { required: true, message: "监理主要工作不能为空", trigger: "blur" }
        // ],
        // wtjclqk: [
        //   { required: true, message: "问题及处理情况不能为空", trigger: "blur" }
        // ],

      }
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList", "nickName", "name", "userId", "deptName"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      this.tbProjectInfo();
      this.getList();
    }
  },
  created () {
    this.tbProjectInfo();
    this.getList();
    console.log(this.deptName);
    // this.getDeptList();
  },
  methods: {
    // 获取项目信息
    tbProjectInfo () {
      if (this.sysProjectId && this.sysProjectList) {
        const res = this.sysProjectList.filter(pro => pro.projectId === this.sysProjectId);
        if (res[0]) {
          this.queryParams.projectId = this.sysProjectId;
          this.queryParams.projectName = res[0].projectName;
          this.form.projectId = this.sysProjectId;
          this.form.projectName = res[0].projectName;
        }
      }
    },
    /** 查询监理日志列表 */
    getList () {
      this.loading = true;
      listLog(this.addDateRange(this.queryParams, this.dateRange, 'Rq')).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取监理单位下拉
    getDeptList () {
      const params = {
        deptType: 3
      };
      listDeptByType(params).then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        jljg: this.deptName,
        bh: undefined,
        llr: this.nickName,
        shry: undefined,
        rq: undefined,
        tqqk: undefined,
        zysgqk: undefined,
        jlzygz: undefined,
        wtjclqk: undefined,
        fileId: undefined,
        fileIds: [],
        fileIdOld: [],
      };
      this.dateRange = [];
      this.resetForm("form");
      this.tbProjectInfo();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加监理日志";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getLog(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改监理日志";
        if (this.form.fileId) {
          this.form.fileIdOld = this.form.fileId.split(',');
        }
      });
    },
    setDeptName () {
      let info = {};
      info = this.deptOptions.find(item => item.deptId === this.form.jldwId);
      this.form.jldwName = info.deptName;
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fileId) {
            this.form.fileIds = this.form.fileId.split(',');
          }
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除监理日志编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delLog(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    onPrint (row) {
      this.$refs.printDialogRef.init(row.id);
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep {
  .descriptions-label {
    width: 120px;
    text-align: right !important;
    padding: 5px 0 !important;
  }

  .descriptions-content {
    padding: 5px !important;

    .el-form-item {
      margin-bottom: 0px !important;
    }
  }
}
</style>
